/*
 * Copyright (C) 2009 Christoph Caks
 *
 * This file is part of GLWAK.
 *
 * GLWAK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GLWAK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GLWAK.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * X11Window.h
 *
 *  Created on: 25.06.2009
 *      Author: Christoph Caks
 */

#ifndef X11WINDOW_H_
#define X11WINDOW_H_

#include "Log.h"

#include "Window.h"

#include <iostream>
#include <sstream>
#include <vector>

#include "OpenGL.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/xf86vmode.h>


/*
 * GLX_ARB_create_context
 */
/*#define GLX_ARB_create_context
#ifndef GLX_ARB_create_context
#define GLX_ARB_create_context 1

// Accepted as an attribute name in <*attrib_list>
#define GLX_CONTEXT_MAJOR_VERSION_ARB           0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB           0x2092
#define GLX_CONTEXT_FLAGS_ARB                   0x2094

// Accepted as bits in the attribute value for GLX_CONTEXT_FLAGS in <*attrib_list>
#define GLX_CONTEXT_DEBUG_BIT_ARB               0x0001
#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002

extern GLXContext glXCreateContextAttribs( Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list );
#endif /* GLX_ARB_create_context */

typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);

namespace glwak {

namespace priv {

class X11Window: public Window {
public:
	X11Window();
	virtual ~X11Window();

	// from Window
	void startMessageLoop();
	void startMessageThread();
	void hide();
	void show();
	size_t getWindowHandle();

	static void doMessageLoop( X11Window* ctx );

	// from Disposable
	void dispose();

	// own
	::Display *getDisplay();
	::Window getWindow();

private:

	// from Window
	void initializeImpl();
	void reinitializeImpl();

	/**
	 * starts fullscreen mode by sending the appropriate msg to the window manager.
	 */
	void fullscreen();
	/**
	 * restores the normal window state.
	 */
	void restore();

	int modeNum;
	XF86VidModeModeInfo **modes;

	void initVideoModes();

	XF86VidModeModeInfo *findModeForResolution( int xres, int yres );

	void setVideoMode( XF86VidModeModeInfo *mode );
	void restoreVideoMode();

	// own
	::Display *display;
	::Window window;

};

} // namespace priv

} // namespace glwak

#endif /* X11WINDOW_H_ */
